package com.hakgc.rent.web.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 过滤器
 * urlPatterns = "/*" 针对项目下的所有请求都进行过滤
 * @author Xing.Jiejian
 * @date 2020/11/17
 */
@WebFilter(filterName = "EncodingFilter",urlPatterns = "/*")
public class EncodingFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        // 执行从发起请求到进入Servlet之前的过滤操作
        // 设置所有的请求编码格式都为utf-8
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        // 调用下一个过滤器或者对应的Servlet
        chain.doFilter(req, resp);
        // 执行Servlet响应到客户端前的过滤操作
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
